//
//  HTClassPhoneTypeSelectView.swift
//  QuitSmokingApp
//  
//  Created by wealon on 2024.
//  QuitSmokingApp.
//  
    

import UIKit

class HTClassPhoneTypeSelectView: UIView {
    lazy var var_photo_button: UIButton = {
        let var_button = UIButton()
        var_button.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        var_button.setTitle("Take a photo", for: .normal)
        var_button.setTitleColor(.black, for: .normal)
        var_button.onTap { [weak self] in
            self?.BLOCK_typeBLOCK?("0")
            self?.ht_dismissAnimated()
        }
        return var_button
    }()
    
    lazy var var_album_button: UIButton = {
        let var_button = UIButton()
        var_button.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        var_button.setTitle("System photo album", for: .normal)
        var_button.onTap { [weak self] in
            self?.BLOCK_typeBLOCK?("1")
            self?.ht_dismissAnimated()
        }
        var_button.setTitleColor(.black, for: .normal)
        return var_button
    }()
    var BLOCK_typeBLOCK: ((String) -> ())?
    static func showSelectView(_ BLOCK_typeBLOCK: ((String) -> ())?) {
        let var_view = HTClassPhoneTypeSelectView()
        var_view.BLOCK_typeBLOCK = BLOCK_typeBLOCK
        var_view.ht_showAnimated()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        ht_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ht_setUI() {
        self.backgroundColor = .white
        self.addSubview(var_photo_button)
        var_photo_button.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(44.auto())
        }

        self.addSubview(var_album_button)
        var_album_button.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(var_photo_button.snp.bottom)
            make.height.equalTo(44.auto())
        }
        
        let var_lin = UILabel()
        var_lin.backgroundColor = UIColor("#E6E6E6")
        self.addSubview(var_lin)
        var_lin.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(14.auto())
            make.right.equalToSuperview().offset(-14.auto())
            make.top.equalTo(var_photo_button.snp.bottom)
            make.height.equalTo(1)
        }
    }
    
    
    func ht_showAnimated() {
        let var_backgroundView = UIView(frame: UIScreen.main.bounds)
        var_backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
        
        let var_maskBtn = UIButton()
        var_maskBtn.backgroundColor = UIColor.clear
        var_maskBtn.addTarget(self, action: #selector(ht_dismissAnimated), for: .touchUpInside)
        var_backgroundView.addSubview(var_maskBtn)
        var_maskBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        var_backgroundView.addSubview(self)
        self.frame = CGRect(x: 0, y: HTClassTool.STATIC_screenHeight, width: HTClassTool.STATIC_screenWidth, height: 98.auto() + 34)
        self.layoutIfNeeded()
        
        if let var_mainWindow = HTClassTool.STATIC_keyWindow {
            var_mainWindow.addSubview(var_backgroundView)
            UIView.animate(withDuration: 0.3) {
//                self.alpha = 1.0
                self.frame = CGRect(x: 0, y: HTClassTool.STATIC_screenHeight - (98.auto() + 34), width: HTClassTool.STATIC_screenWidth, height: 98.auto() + 34)
            }
        }
    }
    
    @objc func ht_dismissAnimated() {
        UIView.animate(withDuration: 0.3, animations: {
            self.frame = CGRect(x: 0, y: HTClassTool.STATIC_screenHeight, width: HTClassTool.STATIC_screenWidth, height: 98.auto() + 34)
        }) { finished in
            self.superview?.removeFromSuperview()
        }
    }
}
